package net.wesley.android.city;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import android.os.Handler;
import android.os.Message;

import net.wesley.android.City;
import net.yebaihe.sdk.HttpConnection;

public class Nanning extends City {

	private Handler handler=new Handler(){
		public void handleMessage(Message message){
			switch (message.what){
			case HttpConnection.DID_ERROR://DID ERROR
				callback.onComplete(-1, "");
				break;
			case HttpConnection.DID_SUCCEED://DID SUCC
				String body=(String)message.obj;
				processResult(body);
				break;
			}
		}
	};

	public void requestParamsFinish(String[] values) {
		try {
			new HttpConnection(ctx,handler,"UTF-8").get("http://www.nn122.com/XQ_wfxq.aspx?chep="+URLEncoder.encode(chepai,"UTF-8")+"&zjh="+values[0]+"&leix=02");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	private void processResult(String result1) {
		int index=result1.indexOf("<CENTER><STRONG>");
		if (index>0){
			callback.onComplete(-100024, "ret:"+result1.substring(index+16).split("<br>")[0]);
			return;
		}
		index=result1.indexOf("身份证明号码");
		if (index>0){
			String str=result1.substring(index).split("如果您对以上结果有异议")[0];
			String[] rows=str.split("<TABLE ");
			String retvalue="";
			for (int i=1;i<rows.length;i++){
				String[] cols=rows[i].split("<TD");
				String shijian=cols[3].split("</TD>")[0].split(">")[1].replace("&nbsp;", "").trim();
				String didian=cols[7].split("</TD>")[0].split(">")[1].replace("&nbsp;", "").trim();
				String code=cols[11].split("</TD>")[0].split(">")[1].replace("&nbsp;", "").trim();
				retvalue+=String.format("shijian:%s\ndidian:%s\ncode:%s\n\n",shijian,didian,code);
			}
			callback.onComplete(rows.length-1, retvalue);
		}
		else{
			callback.onComplete(0, "");
		}
	}

	@Override
	public boolean match(String chepai) {
		return chepai.startsWith("桂A"); 
	}

	@Override
	public String[] paramDesc() {
		String[] s={"证件号码"};
		return s;
	}

}
